<?php
/*
 * 会员类
 * @arrange Mark
 * @package ixdcw
 * @file    member.class.php
 * @time    12-12-19 上午11:48
 */
defined('IN_IXDCW') or exit('Access Denied');
/**
 * 行业圈会员
 */
class memberIndustry {
    /**
     * @var 用户id
     */
    public $userid;

	public $info;

    /**
     * @var 数据库连接类
     */
    public $db;

    /**
     * @var string member表
     */
    public $table_industry_id;

    /**
     * @var string company 表
     */
    public $table_industry_info;

	public $table_industry_info_fields;
    /**
     * @var string  调用常量的显示, 错误的提示
     */
    public $errmsg = errmsg;

    function __construct() {
		global $db;
		$this->table_industry_id = $db->getPre().'industry_id';
		$this->table_industry_info = $db->getPre().'industry_info';
	    $this->table_industry_info_fields = array('icompany', 'ibuy', 'isell', 'idepartment', 'icareer','edittime');
		$this->db = &$db;
    }

	function get() {
		$this->info  = $this->db->getOne("select * from {$this->table_industry_info} where userid={$this->userid}");
		if ($this->info) {
			$this->info['icats'] = $this->db->getCol("select industry from {$this->table_industry_id} where userid={$this->userid}", 'industry');
		}
		return $this->info;
	}

	function updateiCats($icats, $areaid) {
		if ($icats) {
			$this->db->query("delete from {$this->table_industry_id} where userid = {$this->userid} ");
			$sql = "insert into {$this->table_industry_id}(userid, industry, areaid) values";
			foreach($icats as $v) {
				$sql .= "({$this->userid}, $v, $areaid),";
			}
			$sql = rtrim($sql, ',');
			return $this->db->query($sql);
		} else {
			$this->db->query("delete from {$this->table_industry_id} where userid = {$this->userid}");
			return true;
		}
	}

	function updateInfo($post) {
		if ($this->info) {
			$infos = array();
			foreach($this->table_industry_info_fields as $v) {
				$infos[$v] = $post[$v];
			}
			if ($infos) {
				$sql = "update {$this->table_industry_info} set ";
				foreach($infos as $k => $v) {
					$sql .= " $k='$v',";
				}
				$sql = rtrim($sql, ',') . " where userid={$this->userid}";
				$this->db->query($sql);
			}
		} else {
			$sql = "insert into {$this->table_industry_info} (ibuy, icareer, icompany, idepartment, isell, userid,edittime)
				values ('{$post['ibuy']}','{$post['icareer']}','{$post['icompany']}','{$post['idepartment']}','{$post['isell']}',$this->userid,'{$post['edittime']}')";
			$this->db->query($sql);
		}

	}

	function updateMerchant($post) {
		global $db;

		//从名企类别id  导成 企业库类别id
		foreach($post['catid'] as $key=>$val) {
			if($val) {
				$catname = CategoryModule::getCacheCategoryName(4,$val);
				foreach(PCache::read('category-25.php') as $val) {
					if($val['catname']==$catname){
						$catid = 0;
						$catid=$val['catid'];
						break;
					}
				}
				$cates[] = $catid;
			}
		}

		//公司名称
		$title = $post['company'];

		//地区id
		$company_areaid = $post['areaid'];

		//资金类型
		$company_unit = $post['regunit'];

		//资金
		$company_money = $post['capital'];

		//年份
		$company_year = $post['regyear'];

		//地址
		$company_address = $post['address'];

		//选项参数
		$MOD_MEMBER = PCache::read('module-2.php');
		$types = explode('|', $MOD_MEMBER['com_type']);
		$sizes = explode('|', $MOD_MEMBER['com_size']);

		//公司规模
		foreach($sizes as $key => $val) {
			if($post['size'] =$val){
				$company_size = $key;
				break;
			}
		}
		$did = CompanyEntry::getDocId($title);


		$sql = Sql::update(Table::name(Table::NAME_MERCHANT_DOC), array(
			'company_areaid' => $company_areaid,
			'company_address' => $company_address,
			'company_size' => $company_size,
			'company_unit' => $company_unit,
			'company_money' => $company_money,
			'company_year' => $company_year,

		), 'did='.$did);

		//更新企业库词条
		$db->query($sql);

		$sql = Sql::update(Table::name(Table::NAME_MERCHANT_EDITION), array(
			'company_areaid' => $company_areaid,
			'company_address' => $company_address,
			'company_size' => $company_size,
			'company_unit' => $company_unit,
			'company_money' => $company_money,
			'company_year' => $company_year,
		), 'did='.$did);

		//更新企业库版本
		$db->query($sql);

		return true;
	}


    /**
     * @param $e
     * @return bool 错误赋值
     */
    function _($e) {
		$this->errmsg = $e;
		return false;
	}
}